<?php
class news_type extends database{
// add ------
	public function Add($name, $status, $sort){				
		if(!$this->isText($name)){
			return 2;
		}else if(!$this->isLen($name,1,60)){
			return 3;
		}else if(!$this->isInt($status)){
			return 4;
		}else if(!$this->isInt($sort)){
			return 5;
		}else{			
			$sql="insert into news_type(name, status, sort) values(N'".$name."', ".$status.", ".$sort.")";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// edit ------
	public function Edit($id_type, $name, $status, $sort){				
		if(!$this->isInt($id_type)){
			return 2;
		}else if(!$this->isText($name)){
			return 3;
		}else if(!$this->isLen($name,1,60)){
			return 4;
		}else if(!$this->isInt($status)){
			return 5;
		}else if(!$this->isInt($sort)){
			return 6;
		}else{			
			$sql="update news_type set name=N'".$name."', status=".$status.", sort=".$sort." where id_type=".$id_type."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// Del ------
	public function Del($id_type){				
		if(!$this->isInt($id_type)){
			return 2;
		}else{			
			$sql="delete from news_type where id_type=".$id_type."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// get by id
	public function GetById($id_type){	
		if(!$this->isInt($id_type)){
			return 2;
		}else{	
			$sql="select id_type, name, status, sort from news_type where id_type=".$id_type."";
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get all
	public function GetByAll($status=10, $sort=0){	
		if(!$this->isInt($status)){
			return 2;
		}else if(!$this->isInt($sort)){
			return 3;
		}else{	
			if($status==10){
				$status	= "status<>2";	
			}else{
				$status	= " status=".$status;	
			}
			if($sort==0){
				$sort	= " order by sort ";	
			}else{
				$sort	= " order by sort desc";	
			}
			$sql="select id_type, name, status, sort from news_type where ".$status." ".$sort;
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get MaxId
	public function GetMaxId(){			
		$sql="select max(id_type) as id_type from news_type";
		if($result=$this->query($sql)){
			$a=$this->fetch($result);
			return $a['id_type'];
		}
		return 0;
	}
}
?>